﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.sprite;
using imaginecup.util;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util.resourceMan;
using System.Diagnostics;

namespace Imaginecup.gameState.water.mapMan
{
    public class ScrollTexture
    {
        ISprite m_Sprite;//그려질 텍스쳐
        Rectangle infoSource;//그려질 위치
        Rectangle infoDisplayCurrent;//실제 그려질 위치
        Rectangle infoDisplayNext;
        Point startPosition;//그려질 시작 위치
        Point scrollDirection;//스크롤방향
        Point scrollSpeed;//스크롤속도

        Vector2 scrollDirectionAndSpeed;
        Vector2 edgeStartPosition;

        Rectangle destinationRect = new Rectangle(0, 0, 800, 480);

        [Flags]
        public enum Direction
        {
            None = 0,

            Left = 1,
            Right = 2,

            Up = 4,
            Down = 8,

            LeftUp = Left | Up,
            LeftDown = Left | Down,

            RightUp = Right | Up,
            RightDown = Right | Down,
        }

        public ScrollTexture(ISprite sprite, EntityInfo display, Direction direction)
        {
            InitializeInformation(sprite, display, direction);
        }

        public void InitializeInformation(ISprite sprite, EntityInfo display, Direction direction)
        {
            m_Sprite = sprite;

            infoSource = new Rectangle(0, 0, (int)display.mSize.X, (int)display.mSize.Y);

            infoDisplayCurrent.X = (int)display.mPosition.X;
            infoDisplayCurrent.Y = (int)display.mPosition.Y;
            infoDisplayCurrent.Width = (int)display.mSize.X;
            infoDisplayCurrent.Height = (int)display.mSize.Y;

            infoDisplayNext.X = (int)display.mPosition.X;
            infoDisplayNext.Y = (int)display.mPosition.Y;
            infoDisplayNext.Width = (int)display.mSize.X;
            infoDisplayNext.Height = (int)display.mSize.Y;

            startPosition.X = (int)display.mPosition.X;
            startPosition.Y = (int)display.mPosition.Y;

            scrollDirection = new Point(0, 0);//스크롤 기본 방향 설정

            scrollSpeed = new Point(8, 8);
            scrollDirectionAndSpeed = Vector2.Zero;

            edgeStartPosition = new Vector2((int)display.mPosition.X, (int)display.mPosition.Y);
        }

        public Direction PointToDirection(Point direction)
        {
            Direction result = Direction.None;

            if (-1 == direction.X)
            {
                result = result | Direction.Left;
            }
            else if (1 == direction.X)
            {
                result = result | Direction.Right;
            }

            if (-1 == direction.Y)
            {
                result = result | Direction.Up;
            }
            else if (1 == direction.Y)
            {
                result = result | Direction.Down;
            }
            return result;
        }
        public void SetDirection(Point direction)
        {
            if (direction.X > 0)
                scrollDirection.X = 1;
            else if (direction.X < 0)
                scrollDirection.X = -1;
            else
                scrollDirection.X = 0;

            if (direction.Y > 0)
                scrollDirection.Y = 1;
            else if (direction.Y < 0)
                scrollDirection.Y = -1;
            else
                scrollDirection.Y = 0;            
        }

        public void Update(GameTime gameTime)
        {
            startPosition.X = startPosition.X + scrollDirection.X * scrollSpeed.X;
            startPosition.Y = startPosition.Y + scrollDirection.Y * scrollSpeed.Y;

            scrollDirectionAndSpeed.X = scrollDirection.X * scrollSpeed.X;
            scrollDirectionAndSpeed.Y = scrollDirection.Y * scrollSpeed.Y;

            edgeStartPosition.X = edgeStartPosition.X + scrollDirection.X * scrollSpeed.X;
            edgeStartPosition.Y = edgeStartPosition.Y + scrollDirection.Y * scrollSpeed.Y;

            Debug.WriteLine("edgeStartPoint : " + edgeStartPosition);

            //////////////////////////////////////////////////////////////////////////
            infoDisplayCurrent.X = infoDisplayCurrent.X + scrollDirection.X * scrollSpeed.X;
            infoDisplayCurrent.Y = infoDisplayCurrent.Y + scrollDirection.Y * scrollSpeed.Y;
            infoDisplayNext.Y = infoDisplayNext.Y + scrollDirection.Y * scrollSpeed.Y;

            if (infoDisplayCurrent.X > -600)//왼쪽
                infoDisplayNext.X = infoDisplayCurrent.X - 2000;
            else if (infoDisplayCurrent.X < -600)
                infoDisplayNext.X = infoDisplayCurrent.X + 2000;

            if (infoDisplayNext.X > 0 && infoDisplayCurrent.X > 2000)//왼쪽
                infoDisplayCurrent.X = infoDisplayNext.X;
            else if (infoDisplayNext.X < -1200 && infoDisplayCurrent.X < -3200)
                infoDisplayCurrent.X = infoDisplayNext.X;

            if (infoDisplayCurrent.Y >= 0)
            {
                infoDisplayCurrent.Y = 0;
                infoDisplayNext.Y = 0;
            }
            else if (infoDisplayCurrent.Y <= -720)
            {
                infoDisplayCurrent.Y = -720;
                infoDisplayNext.Y = -720;
            }
            //////////////////////////////////////////////////////////////////////////

            if(edgeStartPosition.X > 0)
            {
                edgeStartPosition.X = 0;
                startPosition.X = 0;
                scrollDirectionAndSpeed = Vector2.Zero;
            }
            else if(edgeStartPosition.X < -1200)
            {
                edgeStartPosition.X = -1200;
                startPosition.X = 800;
                scrollDirectionAndSpeed = Vector2.Zero;
            }

            if(edgeStartPosition.Y > 0)
            {
                edgeStartPosition.Y = 0;
                startPosition.Y = 0;
                scrollDirectionAndSpeed = Vector2.Zero;
            }
            else if(edgeStartPosition.Y < -720)
            {
                edgeStartPosition.Y = -720;
                startPosition.Y = 480;
                scrollDirectionAndSpeed = Vector2.Zero;
            }
        }

        public void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            EntityInfo info = new EntityInfo();
            info.mPosition.X = infoDisplayCurrent.X;
            info.mPosition.Y = infoDisplayCurrent.Y;
            info.mSize.X = infoDisplayCurrent.Width;
            info.mSize.Y = infoDisplayCurrent.Height;

            m_Sprite.Draw(spriteBatch, info);

            info.mPosition.X = infoDisplayNext.X;
            info.mPosition.Y = infoDisplayNext.Y;
            info.mSize.X = infoDisplayNext.Width;
            info.mSize.Y = infoDisplayNext.Height;

            m_Sprite.Draw(spriteBatch, info);
        }
    }
}
